package com.gezhi.stuserver.stumag.controller;


import com.gezhi.stuserver.bean.StudentBean;
import com.gezhi.stuserver.dto.ResultData;
import com.gezhi.stuserver.stumag.service.IStudentService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * 学生控制器
 *
 * @author AWG
 */
@RequestMapping(value = "/stus")
@RestController
public class StudentController {

	@Resource
	private IStudentService studentServiceImpl;

	/**
	 * 新增学生
	 * @param studentBean 学生对象
	 */
	@PostMapping("/list")
	public ResultData saveMember(StudentBean studentBean){
		studentServiceImpl.saveStudent(studentBean);
		ResultData resultData = new ResultData();
		resultData.setCode("0");
		resultData.setMsg("操作成功");
		return resultData;
	}

	/**
	 * 修改学生
	 * @param studentBean 学生对象
	 */
	@PutMapping("/{id}")
	public ResultData updateMember(@PathVariable("id") Integer id, StudentBean studentBean){
		studentBean.setId(id);
		studentServiceImpl.updateStudent(studentBean);
		ResultData resultData = new ResultData();
		resultData.setCode("0");
		resultData.setMsg("操作成功");
		return resultData;
	}

	/**
	 * 删除学生
	 * @param studentBean 学生对象
	 */
	@DeleteMapping("/{id}")
	public ResultData deleteMember(@PathVariable("id") Integer id, StudentBean studentBean){
		studentBean.setId(id);
		studentServiceImpl.deleteStudent(studentBean);
		ResultData resultData = new ResultData();
		resultData.setCode("0");
		resultData.setMsg("操作成功");
		return resultData;
	}

	/**
	 * 根据学生ID查询学生
	 * @param id 学生ID
	 */
	@GetMapping("/{id}")
	public ResultData getMemberById(@PathVariable("id") Integer id){
		StudentBean studentBean = studentServiceImpl.getStudentById(id);
		ResultData resultData = new ResultData();
		resultData.setCode("0");
		resultData.setMsg("操作成功");
		resultData.setData(studentBean);
		return resultData;
	}

	/**
	 * 查询所有的学生
	 * @return 所有的学生组成的List集合
	 */
	@GetMapping("/list")
	public ResultData findAllStudent(){
		List<StudentBean> studentBeans = studentServiceImpl.findAllStudent();
		ResultData resultData = new ResultData();
		resultData.setCode("0");
		resultData.setMsg("操作成功");
		resultData.setData(studentBeans);
		return resultData;
	}


}
